# guess.r
# J. Kirk 23-07-09
# A simulation solution to a problem posed in
# "Digital Dice" by Paul J. Nahin
# From p.5:
# Suppose a student is given a list of the 24 US Presidents
# from the 19th Century and another list of their terms in office, but scrambled
# If the student has to guess every time, oh average, how many times will
# the student guess correctly?

# Assume that the student assigns terms to presidents with a one-to-one
# relationship, that is no president or term is used more than once.

# M is the length of the president/term lists
M <- 24
totalcorrect <- 0

for (k in 1:1000000)
{
	term <-sample(M)
	for (j in 1:M)
	{
		if (term[j] == j) totalcorrect <- totalcorrect + 1
	}
}

print(totalcorrect/1000000)

#Some notes on the program and it's functions:
# In R, assigment is made with <-.
# The sample() function is highly useful. In this case, it is sampling withOUT
# replacement a list that ranges from 1 to M, with a sample size of M. That is,
# it generates random permutations of M.
